<template>
  <span style="padding-left:10px">
    <el-button :size="size" plain :type="type" @click="copyRole">
      <fa-icon v-show="icon" class="fa-copy" />{{title}}</el-button>
    <el-dialog title="复制职务" :visible.sync="dialog" width="24%" :close-on-click-modal="false" append-to-body>
      <add-or-edit-role v-if="dialog" :editData="editData" @copy="copy" @close="dialog = false" />
    </el-dialog>
  </span>
</template>

<script>
import api from '../api'
import { CONSTANT_OBJ } from '@/utils/constant'
import { AddOrEditRole } from './index'
export default {
  props: {
    title: {
      type: String,
      default: '复制'
    },
    icon: {
      type: Boolean,
      default: true
    },
    size: {
      type: String,
      default: 'small'
    },
    type: {
      type: String,
      default: ''
    },
    roleData: Object
  },
  components: { AddOrEditRole },
  data () {
    return {
      dialog: false,
      editData: {},
      roleDataDetail: {},
      systemPermissionVos: {}
    }
  },
  methods: {
    // 获取职务详细数据
    getRoleData () {
      api.getRoleDetailData(this.roleData.roleId).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.roleDataDetail = data || {}
        }
      })
    },
    // 获取角色操作权限
    getPermission () {
      api.getRolePermission(this.roleData.roleId).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.systemPermissionVos = data.systemPermissionVos
        }
      })
    },
    // 复制职务基础信息
    copyRole () {
      if (this.roleData && this.roleData.dataPermission) {
        this.roleDataDetail = this.roleData
      } else {
        this.getRoleData()
      }
      this.getPermission()
      this.editData = { ...this.roleData, isCopy: true }
      this.dialog = true
    },
    // 复制职务权限
    async copy (data) {
      this.copyRoleData = data
      console.log(data, '复制')
      if (data) {
        this.dialog = false
        this.$emit('uppdataSuccess')
      }
    }
    // // 复制数据权限
    // async copyDataPermission () {
    //   const { roleId, revision } = this.copyRoleData
    //   const { dataPermission } = this.roleDataDetail
    //   const res = await api.postChangeDataPermissionsData({ roleId, revision, dataPermission })
    //   return res
    // },
    // // 复制操作权限
    // async copySystemPermission () {
    //   const { roleId } = this.copyRoleData
    //   const { systemPermissionVos } = this
    //   const res = await api.postChangeSystemPermissionsData({
    //     roleId,
    //     systemPermissionVos
    //   })
    //   return res
    // }
  }
}
</script>
